package com.woniuxy.rabbitmq.receiver;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rabbitmq.client.Channel;
import com.woniuxy.service.AuctionService;
import com.woniuxy.util.JsonUtil;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @Author: wanghao
 * @Date: 2019/9/27 14:54
 */
@Component
public class EndReceiver {
    @Resource
    private AuctionService auctionService;
    @RabbitListener(queues = {"delayedEndQueue"}, containerFactory = "rabbitListenerContainerFactory")
    public void handleMessage(Message message, Channel channel) throws Exception {
        Map<String,Integer> map = new ObjectMapper().readValue(new String(message.getBody()), Map.class);
        if (map == null) {
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
        }
        Integer aid = map.get("aid");
        // 处理消息
        System.out.println("拍卖结束消费 :" + aid);
        Boolean isAuctionOk = auctionService.auctionEnd(aid);
        if (isAuctionOk) {
            System.out.println("拍卖结束成功 :" + aid);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } else {
            channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,true);
        }
    }
}
